# -*- coding: utf-8 -*-


import random
import os, sys
import time
import subprocess

import pygame


BLUE = (0, 137, 242)
BLUE_PRESSED = (38, 226, 255)
RED = (255, 52, 50)
PURPLE = (171, 112, 255)
GREEN = (0, 255, 33)
WHITE = (242, 244, 246)
BLACK = (18, 17, 16)


def drawText(
    surface: pygame.Surface,
    text: str,
    color: tuple,
    position: tuple,
    size: int,
    isBold=False,
    isItalic=False,
    font="DengXian",
    isAntialias=True,
    bg=None,
) -> None:
    """在`surface`上写下`text`

    Parameter
    --------
    surface: `pygame.Surface`
        pygame Surface对象
    text: `str`
        显示的文字
    color: `tuple`
        颜色数组(RGB)或(RGBA)
    position: `tuple`
        文字左上角的位置
    size: int
        文字字号(以像素为准)
    isBold: `bool`
        是否加粗 默认为 `False`
    isItalic: `bool`
        是否斜体 默认为 `False`
    font: `str`
        字体 默认为 `DengXian`
    isAntialias: `bool`
        是否使用平滑字体 默认为 `True`
    bg: `tuple`
        背景颜色 默认为 `None`，即无背景色
    """
    surface.blit(
        pygame.font.SysFont(font, int(size), isBold, isItalic).render(
            text, isAntialias, color, bg
        ),
        (
            int(position[0]),
            int(position[1]),
        ),
    )


def drawLineChart(
    surface: pygame.Surface,
    datas: list,
    LAunit: str = "次",
    HAunit: str = "秒",
    position: tuple = (50, 500),
    size: tuple = (500, 250),
    color: tuple|int = WHITE,
    textColor: tuple = BLUE,
    draw_H_data: bool = False,
    draw_L_data: bool = False,
):
    """在`surface`上的`position`位置，以`datas`作为数据，绘制，大小为`size`，颜色为`color`的折线统计图

    Parameters
    ---------
    surface: `pygame.Surface`
        pygame Surface对象
    datas: `list[int|float..]`
        用于绘制表格的数据
    LAunit: `str`
        绘制在纵轴上的数据单位
    HAunit: `str`
        绘制在横轴上的数据单位
    position: `tuple`
        绘制表格的左上角的位置
    size: `tuple`
        绘制表格的最大大小
    color: `tuple`
        折线颜色
    textColor: `tuple`
        绘制文字的颜色
    """

    if len(datas) < 2:
        return

    # 纵轴线
    pygame.draw.aaline(
        surface,
        color,
        (position[0] + size[0] / 50, position[1]),
        (position[0] + size[0] / 50, position[1] + size[1]),
        blend=1,
    )

    # 横轴线
    pygame.draw.aaline(
        surface,
        color,
        (position[0], position[1] + size[1] - size[1] / 25),
        (position[0] + size[0], position[1] + size[1] - size[1] / 25),
        blend=1,
    )

    pointlist = []
    x_increment = size[0] * 49 / 50 / len(datas)
    x = position[0] + size[0] / 50
    ymax = max(datas)
    # 横排数据y值
    y_HA = position[1] + size[1] - size[1] / 25
    for i in datas:
        # 画线点阵
        pointlist.append(
            (
                int(x + 0.5),
                (
                    int(
                        position[1]
                        + size[1] * 24 / 25
                        - (size[1] * 24 / 25) / ymax * i
                        + 0.5
                    )
                    if ymax != 0
                    else 0
                ),
            )
        )
        # 横排数据
        if draw_H_data:
            drawText(
                surface,
                str(int(x / x_increment)),
                textColor,
                (int(x + 0.5), y_HA),
                size[1] / 25,
            )
        # 纵列数据
        if draw_L_data:
            if i != 0:
                drawText(
                    surface,
                    str(i),
                    textColor,
                    (
                        int(x),
                        (
                            int(
                                position[1]
                                + size[1] * 11.5 / 12.5
                                - (size[1] * 24 / 25) / ymax * i
                                + 0.5
                            )
                            if ymax != 0
                            else 0
                        ),
                    ),
                    size[1] / 25,
                    True,
                )
        x += x_increment

    # 绘制单位

    # 横轴末尾
    drawText(
        surface,
        HAunit,
        textColor,
        (position[0] + size[0], y_HA),
        size[1] / 20,
        True,
        True,
    )
    # 纵轴末尾
    drawText(
        surface,
        LAunit,
        textColor,
        (position[0] - len(LAunit) * size[1] / 40, position[1] - 0.5),
        size[1] / 20,
        True,
        True,
    )

    # 图表
    pygame.draw.aalines(surface, color, False, pointlist)


print("正在测试...")

pygame.init()

# 初始设置
screen = pygame.display.set_mode((1080, 720), pygame.RESIZABLE)  # Pygame窗口
pygame.display.set_caption("Random测试")  # 标题

res_keys = [i for i in range(100)]
final_res = [0 for i in range(100)]
testtime = 0
ttime = time.time()

screen.fill(BLACK)
colour = 0x000000

while True:

    btime = time.time()

    for i in range(1000):
        final_res[random.choice(res_keys)] += 1

    atime = time.time()

    events = pygame.event.get()
    if events:
        for event in events:  # 遍历事件
            if event.type == pygame.QUIT:  # 退出事件
                # print("EXIT")
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()

    # screen.fill(BLACK)
    pygame.draw.rect(
        screen,
        BLACK,
        (
            0,
            int(screen.get_height() / 16),
            screen.get_width(),
            int(screen.get_height() / 11),
        ),
    )

    # drawText(
    #     screen,
    #     f"红包{money}元{amount}个  测试次数：{testtime}\t平均用时：{round(sum(timelist)/timelist.__len__()*100000)/100}ms",
    #     WHITE,
    #     (0, 0),
    #     int(screen.get_height() / 24),
    # )
    # drawText(
    #     screen,
    #     f"总数核对出错率：{round(amountCheckTimes/testtime*10000)/100}%\t金额出错率：{round(errorTimes/testtime*10000)/100}%\t超时率：{round(timeErrorTimes/testtime*10000)/100}",
    #     WHITE,
    #     (0, int(screen.get_height() / 24)),
    #     int(screen.get_height() / 48),
    # )

    drawLineChart(
        screen,
        final_res,
        "频数",
        "下标",
        (int(screen.get_height() / 12), int(screen.get_height() / 4)),
        (
            screen.get_width() - int(screen.get_height() / 4),
            int(screen.get_height() / 16 * 9),
        ),
        colour := colour + 32
    )

    # drawLineChart(
    #     screen,
    #     [round(sum(i)/len(i)*10000)/100 for i in saves],
    #     "平均金额比/%",
    #     "第/个",
    #     (int(screen.get_height() / 10), int(screen.get_height() / 8 * 3)),
    #     (
    #         screen.get_width() - int(screen.get_height() / 5.5),
    #         int(screen.get_height() / 2),
    #     ),
    # )

    drawText(
        screen,
        f"正在测试第 {testtime} 次随机 绘图时间 {round((time.time() - atime)*100000)/100} 毫秒",
        WHITE,
        (0, int(screen.get_height() / 16)),
        int(screen.get_height() / 24),
    )

    drawText(
        screen,
        f"单次循环时间 {round((time.time() - btime)*100000)/100} 毫秒 总耗时 {round((time.time() - ttime)*100)/100} 秒",
        WHITE,
        (0, int(screen.get_height() / 16) + int(screen.get_height() / 23)),
        int(screen.get_height() / 24),
    )

    pygame.display.update()  # 刷新屏幕

    testtime += 1


pygame.quit()
sys.exit()
